﻿#ifndef BUSINESSMSGHANDLER_H
#define BUSINESSMSGHANDLER_H

#include <Router/router.h>

#include <protocol/protocol.h>
#include <nlohmann/json.hpp>
#include <hv/Channel.h>
#include <UserManager/usermanager.h>

class BusinessMsgHandler
{
public:
    // 删除拷贝构造函数和拷贝赋值操作符
    BusinessMsgHandler(BusinessMsgHandler const&) = delete;
    BusinessMsgHandler& operator=(BusinessMsgHandler const&) = delete;
    // 提供一个全局唯一的接口
    static BusinessMsgHandler& getInstance() {
        static BusinessMsgHandler instance;
        return instance;
    }


    // init the modules
    void init();

    // handle message
    void handleMsg(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

    // chat to user
    void chatToUser(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

    // get json of message
    static nlohmann::json getJsonOfMessage(std::shared_ptr<Protocol> protocol);

    // receive download file request
    void recvDownloadRequest(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

private:
    Router msgRouter;

    BusinessMsgHandler();

    // receive a reply of a apply
    void RecvReplyOfApply(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

    // user login the server
    void loginServer(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

    // init client after login the server
    void initClientAfterLogin(const std::string& account, const hv::SocketChannelPtr& channel);

    // get message of protocol
    std::string decryptMessageOfProtocol(std::shared_ptr<Protocol> protocol, User* user, bool doNotDecrypt = false);

    // query offine message of (a to b)
    void queryOffineMessage(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);

    // return a unique id
    void returnUniqueID(std::shared_ptr<Protocol> protocol, const hv::SocketChannelPtr& channel);





};

#endif // BUSINESSMSGHANDLER_H
